<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>Set Map实现数组去重 | 一枚前端小菜鸟的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="集合数学中的集合集合的三大特性：确定性、互异性、无序性 数组去重123let arr1 &#x3D; [1,2,3,3,4,4]let arr2 &#x3D; [&amp;#123;id:1,value:1&amp;#125;,&amp;#123;id:2,value:2&amp;#125;,&amp;#123;id:3,value:3&amp;#125;,&amp;#123;id:3,value:4&amp;#125;]数组去重可以考虑通过find,includes,findI">
<meta property="og:type" content="article">
<meta property="og:title" content="Set Map实现数组去重">
<meta property="og:url" content="https://gitee.com/weiyayun1995/weiyayun1995.git/2021/06/18/Set-Map%E5%AE%9E%E7%8E%B0%E6%95%B0%E7%BB%84%E5%8E%BB%E9%87%8D/index.html">
<meta property="og:site_name" content="一枚前端小菜鸟的博客">
<meta property="og:description" content="集合数学中的集合集合的三大特性：确定性、互异性、无序性 数组去重123let arr1 &#x3D; [1,2,3,3,4,4]let arr2 &#x3D; [&amp;#123;id:1,value:1&amp;#125;,&amp;#123;id:2,value:2&amp;#125;,&amp;#123;id:3,value:3&amp;#125;,&amp;#123;id:3,value:4&amp;#125;]数组去重可以考虑通过find,includes,findI">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-06-18T05:41:07.000Z">
<meta property="article:modified_time" content="2021-06-18T05:45:18.664Z">
<meta property="article:author" content="yywei">
<meta property="article:tag" content="菜鸟,前端,js,react,vue">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="一枚前端小菜鸟的博客" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">一枚前端小菜鸟的博客</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">yywei的遇坑记录</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS 订阅"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="搜索"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://gitee.com/weiyayun1995/weiyayun1995.git"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-Set-Map实现数组去重" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2021/06/18/Set-Map%E5%AE%9E%E7%8E%B0%E6%95%B0%E7%BB%84%E5%8E%BB%E9%87%8D/" class="article-date">
  <time class="dt-published" datetime="2021-06-18T05:41:07.000Z" itemprop="datePublished">2021-06-18</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      Set Map实现数组去重
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="集合"><a href="#集合" class="headerlink" title="集合"></a>集合</h2><p><a target="_blank" rel="noopener" href="https://baike.baidu.com/item/%E9%9B%86%E5%90%88/2908117?fr=aladdin">数学中的集合</a><br>集合的三大特性：确定性、互异性、无序性</p>
<h2 id="数组去重"><a href="#数组去重" class="headerlink" title="数组去重"></a>数组去重</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr1 = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">4</span>]</span><br><span class="line"><span class="keyword">let</span> arr2 = [&#123;<span class="attr">id</span>:<span class="number">1</span>,<span class="attr">value</span>:<span class="number">1</span>&#125;,&#123;<span class="attr">id</span>:<span class="number">2</span>,<span class="attr">value</span>:<span class="number">2</span>&#125;,&#123;<span class="attr">id</span>:<span class="number">3</span>,<span class="attr">value</span>:<span class="number">3</span>&#125;,&#123;<span class="attr">id</span>:<span class="number">3</span>,<span class="attr">value</span>:<span class="number">4</span>&#125;]</span><br><span class="line">数组去重可以考虑通过find,includes,findIndex,indexOf的方法来判断数组中是否含有某些元素</span><br></pre></td></tr></table></figure>
<p><a target="_blank" rel="noopener" href="https://www.jianshu.com/p/5f8d9db7956f">js数组去重</a></p>
<h2 id="js-中的Set数据结构"><a href="#js-中的Set数据结构" class="headerlink" title="js 中的Set数据结构"></a>js 中的Set数据结构</h2><p><strong>概念：</strong> set是ES6新增的数据结构。set数据结构使用了与有限集合相同的数学概念，应用在计算机的数据结构中，与数组类似，但成员都是唯一的，没有重复的值<br><strong>用法：</strong> </p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 创建一个空的set集合</span></span><br><span class="line"><span class="keyword">const</span> emptySet = <span class="keyword">new</span> <span class="built_in">Set</span>()</span><br><span class="line"><span class="comment">// 添加元素add(注意，add一次只能新增一个元素)</span></span><br><span class="line">emptySet.add(<span class="number">1</span>)</span><br><span class="line"><span class="built_in">console</span>.log(emptySet)</span><br><span class="line"><span class="comment">// 添加多个元素</span></span><br><span class="line"><span class="keyword">let</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br><span class="line">arr.forEach(<span class="function"><span class="params">item</span> =&gt;</span> &#123;emptySet.add(item)&#125;)</span><br><span class="line"><span class="built_in">console</span>.log(emptySet)</span><br><span class="line"><span class="comment">// 删除元素</span></span><br><span class="line">emptySet.delete(<span class="number">1</span>)  <span class="comment">//删除指定数据 返回一个布尔值</span></span><br><span class="line">emptySet.has(<span class="number">1</span>) <span class="comment">// 判断该值是否为set成员，返回一个布尔值</span></span><br><span class="line">emptySet.clear() <span class="comment">// 清除所有数据，没有返回值</span></span><br><span class="line"></span><br><span class="line">arr.forEach(<span class="function"><span class="params">item</span> =&gt;</span> &#123;emptySet.add(item)&#125;)</span><br><span class="line">emptySet.keys() <span class="comment">// 返回键名的遍历器</span></span><br><span class="line">emptySet.values() <span class="comment">// 返回键值的遍历器</span></span><br><span class="line">emptySet.entries() <span class="comment">// 返回键值对的遍历器</span></span><br><span class="line"><span class="comment">//可以使用forEach来调用Set数据结构的每个成员</span></span><br></pre></td></tr></table></figure>
<p><strong>使用Set来进行数组去重</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 普通数组</span><br><span class="line"><span class="keyword">let</span> arr1 = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">4</span>]</span><br><span class="line"><span class="keyword">let</span> newArr1 = <span class="built_in">Array</span>.from(<span class="keyword">new</span> <span class="built_in">Set</span>(arr1)) <span class="comment">// 由于Set集合是一个伪数组需要用Array.from转换下</span></span><br><span class="line"></span><br><span class="line"><span class="number">2.</span> 数组对象去重</span><br><span class="line"><span class="keyword">let</span> arr2 = [&#123;<span class="attr">id</span>:<span class="number">1</span>, <span class="attr">value</span>: <span class="number">1</span>&#125;,&#123;<span class="attr">id</span>:<span class="number">2</span>, <span class="attr">value</span>:<span class="number">2</span>&#125;, &#123;<span class="attr">id</span>:<span class="number">3</span>, <span class="attr">value</span>:<span class="number">3</span>&#125;, &#123;<span class="attr">id</span>:<span class="number">1</span>, <span class="attr">value</span>:<span class="number">1</span>&#125;]</span><br><span class="line"><span class="comment">// 根据id进行去重</span></span><br><span class="line"><span class="keyword">let</span> setId = <span class="keyword">new</span> <span class="built_in">Set</span>()</span><br><span class="line"><span class="keyword">let</span> setItem = <span class="keyword">new</span> <span class="built_in">Set</span>()</span><br><span class="line">arr2.forEach(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (!setId.has(item.id)) &#123;</span><br><span class="line">        setId.add(item.id)</span><br><span class="line">        setItem.add(item)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="js中的Map数据结构"><a href="#js中的Map数据结构" class="headerlink" title="js中的Map数据结构"></a>js中的Map数据结构</h2><p><strong>简介：</strong><br>ES6 提供了 Map 数据结构。它类似于对象，也是键值对的集合，但是“键”的范围不限于字符串，各种类型的值（包括对象）都可以当作键。也就是说，Object 结构提供了“字符串—值”的对应，Map 结构提供了“值—值”的对应，是一种更完善的 Hash 结构实现。如果你需要“键值对”的数据结构，Map 比 Object 更合适。</p>
<p><strong>用法：</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> m = <span class="keyword">new</span> <span class="built_in">Map</span>()</span><br><span class="line"><span class="keyword">const</span>  o = &#123;<span class="attr">p</span>: <span class="string">&#x27;hello world&#x27;</span>&#125;</span><br><span class="line"><span class="comment">// 添加成员</span></span><br><span class="line">m.size() <span class="comment">// map集合的长度length</span></span><br><span class="line">m.set(o, <span class="string">&#x27;content&#x27;</span>) <span class="comment">// 对象当作键</span></span><br><span class="line">m.get(o)</span><br><span class="line">m.has(o) <span class="comment">// true</span></span><br><span class="line">m.delete(o)</span><br><span class="line">m.clear() <span class="comment">// 删除所有键值对</span></span><br><span class="line">m.keys() <span class="comment">// 所有的键集合</span></span><br><span class="line">m.values() <span class="comment">// 所有的值得集合</span></span><br><span class="line">m.entries() <span class="comment">// 所有的键值对得集合</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// Map可以接收一个数组作为参数</span></span><br><span class="line"><span class="keyword">const</span> map = <span class="keyword">new</span> <span class="built_in">Map</span>([[<span class="string">&#x27;name&#x27;</span>,<span class="string">&#x27;张三&#x27;</span>],[<span class="string">&#x27;title&#x27;</span>,<span class="string">&#x27;author&#x27;</span>]])</span><br><span class="line">map.size <span class="comment">// ?</span></span><br><span class="line">map.get(<span class="string">&#x27;name&#x27;</span>) <span class="comment">// ?</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 接收数组作为参数的过程</span></span><br><span class="line"><span class="keyword">const</span> items = [</span><br><span class="line">  [<span class="string">&#x27;name&#x27;</span>, <span class="string">&#x27;张三&#x27;</span>],</span><br><span class="line">  [<span class="string">&#x27;title&#x27;</span>, <span class="string">&#x27;Author&#x27;</span>]</span><br><span class="line">];</span><br><span class="line"><span class="keyword">const</span> map = <span class="keyword">new</span> <span class="built_in">Map</span>();</span><br><span class="line">items.forEach(</span><br><span class="line">  ([key, value]) =&gt; map.set(key, value)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Map数组去重</span></span><br><span class="line"><span class="keyword">let</span> arr2 = [&#123;<span class="attr">id</span>:<span class="number">1</span>, <span class="attr">value</span>: <span class="number">1</span>&#125;,&#123;<span class="attr">id</span>:<span class="number">2</span>, <span class="attr">value</span>:<span class="number">2</span>&#125;, &#123;<span class="attr">id</span>:<span class="number">3</span>, <span class="attr">value</span>:<span class="number">3</span>&#125;, &#123;<span class="attr">id</span>:<span class="number">1</span>, <span class="attr">value</span>:<span class="number">2</span>&#125;]</span><br><span class="line"><span class="comment">// 根据数组项进行去重</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">unique</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> map = <span class="keyword">new</span> <span class="built_in">Map</span>()</span><br><span class="line">    <span class="keyword">return</span> arr.filter( <span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> !map.has(<span class="built_in">JSON</span>.stringify(item)) &amp;&amp; map.set(<span class="built_in">JSON</span>.stringify(item))</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 根据数组对象中指定元素进行去重 例如id</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">unique1</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> map = <span class="keyword">new</span> <span class="built_in">Map</span>()</span><br><span class="line">    <span class="keyword">return</span> arr.filter( <span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> !map.has(item.id) &amp;&amp; map.set(item.id)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://gitee.com/weiyayun1995/weiyayun1995.git/2021/06/18/Set-Map%E5%AE%9E%E7%8E%B0%E6%95%B0%E7%BB%84%E5%8E%BB%E9%87%8D/" data-id="ckq7mlsv40003dgt9176fgngl" data-title="Set Map实现数组去重" class="article-share-link">分享</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2021/06/18/%E8%84%B1%E6%95%8F/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">前一篇</strong>
      <div class="article-nav-title">
        
          脱敏
        
      </div>
    </a>
  
  
    <a href="/2021/06/03/h5%E9%80%9A%E8%BF%87input%E8%B0%83%E7%94%A8%E7%9B%B8%E6%9C%BA/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">后一篇</strong>
      <div class="article-nav-title">h5通过input调用相机</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">六月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/05/">五月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/04/">四月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/03/">三月 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/06/22/js%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E8%BD%AC%E6%8D%A2/">js数据类型及转换</a>
          </li>
        
          <li>
            <a href="/2021/06/18/react-hook-%E9%A1%B9%E7%9B%AE%E4%B8%AD%E5%AE%9E%E8%B7%B5/">react-hook 项目中实践</a>
          </li>
        
          <li>
            <a href="/2021/06/18/%E6%95%B0%E5%AD%97%E7%AD%BE%E5%90%8D/">数字签名</a>
          </li>
        
          <li>
            <a href="/2021/06/18/sendbeacon/">sendbeacon</a>
          </li>
        
          <li>
            <a href="/2021/06/18/js%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%88%A4%E6%96%AD/">js数据类型的判断</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 yywei<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>